package com.eeepay.eeepay_v2.activity;

import android.text.TextUtils;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.TradeDetailsAdapter;
import com.eeepay.eeepay_v2.model.TradeInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.TransApiServiceGrpc;
import cn.eeepay.api.grpc.nano.TransProto;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/3/15.
 * 交易详情界面
 */
public class TradeDetailsActivity extends ABBaseActivity {


    private ListView listView;

    private TradeDetailsAdapter adapter;

    private String orderNo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_trade_details;
    }

    @Override
    protected void initWidget() {

        listView = getViewById(R.id.lv_trade_details);
    }

    @Override
    protected void eventOnClick() {
        orderNo = bundle.getString(Constant.TRADE_QUERY);
        LogUtils.d("TAG", "orderNo = " + orderNo);


        adapter = new TradeDetailsAdapter(this);
        listView.setAdapter(adapter);

        sendHttpRequest(0);

    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                TransProto.TransdetailsReq request = new TransProto.TransdetailsReq();
                request.orderNo = orderNo;
                TransApiServiceGrpc.TransApiServiceBlockingStub stub = TransApiServiceGrpc.newBlockingStub(channel);
                TransProto.TransDetailsRes res = stub.queryTransdetails(request);
                return res;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if (result == null)
                    return;
                TransProto.TransDetailsRes res = (TransProto.TransDetailsRes) result;
                if (!res.resMsg.status) {
                    showToast(res.resMsg.msg);
                    return;
                }
                TransProto.TransDetails details = res.transDetails;
                if (details == null) {
                    finish();
                    return;
                }
                setDatas(details);
            }
        });
    }

    private void setDatas(TransProto.TransDetails res) {

        TradeInfo info0 = new TradeInfo("订单编号：", res.orderNo);
        TradeInfo info1 = new TradeInfo("交易时间：", res.transTime);
        TradeInfo info2 = new TradeInfo("交易类型：", res.transType);
        TradeInfo info3 = new TradeInfo("服务种类：", res.serviceType);
        TradeInfo info4 = new TradeInfo("商户名称：", res.merchantName);
        TradeInfo info5 = new TradeInfo("币种：", res.currencyType);
        TradeInfo info6 = new TradeInfo("金额：", res.transAmount);
        TradeInfo info7 = new TradeInfo("商户服务费率：", res.merchantRate);
        TradeInfo info8 = new TradeInfo("商户手续费：", res.merchantFee);
//        TradeInfo info9 = new TradeInfo("本代理商分润规则：", res.getProfitRule());
        TradeInfo info10 = new TradeInfo("本代理商分润收入：", res.profits);
        TradeInfo info11 = new TradeInfo("终端号：", res.terminalNo);
        String accountNo = null;
        if (!TextUtils.isEmpty(res.accountNo)) {
            accountNo = "************" + res.accountNo.substring(res.accountNo.length() - 4);
        }
        TradeInfo info12 = new TradeInfo("交易卡号：", accountNo);
        TradeInfo info13 = new TradeInfo("卡种：", res.cardType);
        TradeInfo info14 = new TradeInfo("卡类型：", res.cardName);
        TradeInfo info15 = new TradeInfo("发卡行：", res.bankName);

        List<TradeInfo> infoList = new ArrayList<>();
        infoList.add(info0);
        infoList.add(info1);
        infoList.add(info2);
        infoList.add(info3);
        infoList.add(info4);
        infoList.add(info5);
        infoList.add(info6);
        infoList.add(info7);
        infoList.add(info8);
//        infoList.add(info9);
//        infoList.add(info10);
        infoList.add(info11);
        infoList.add(info12);
        infoList.add(info13);
        infoList.add(info14);
        infoList.add(info15);

        adapter.setList(infoList);
    }
}
